Syntax of class:
Private | public | global “Virtual | abstract | with sharing | without sharing | (none)” Class implements | (none)” “extends | (none)” { // the body of the class }
To create an Apex class, go to the following path Your Name ---> SetupàApp setup--->Develop ---> click on Apex classes ---> click on "New" button.
*In the editor pane, enter the following code Public class Helloworld { }
Click on Quicksave ---> Saves the Apex Code, making it available to be executed ---> it makes us add and modify code. Save --> savers the Apex Code, but that classes the class editor and returns to the Apex classes list.
*Add the static method to the above class Now Apex class become. Publish class Helloworld { public static void sayyou( ) { System.debug ( 'you' ); }
*Now add an instance method. Now the Apex class became public class Helloworld
{// class creation Public static void sayyou () { //adding static method to the class } Public void sayMe ( ) { //adding instance method to system debug (‘Me '); the class
We have created the "Helloworld" class follow these steps to call its methods.
*** To call a static method, we don't have to create an instance of the class. Helloworld say you (); Open Raw log, then "You" appears in the USER-DEBUG log.
Note:- To call an instance method, we first have to create an instance of the Helloworld class.
Helloworld h= new Helloworld ( ); h.say Me ( );
Open Raw log, then "Me" appears in the USER-DEBUG log.
Inclined to build a profession as SalesForce Developer? Then here is the blog post on, explore SalesForce Training
We can also create new Apex classes directly in the Developer Console.
public static string hellowMessage () { return ( 'welcome to capital info solutions '): }
Examples 1:
Public class AccountCreation { Public Account createAccount(String name){ Account a= new Account(); a.Name = name; insert a; return a; } } Go to the Developer console, and execute the following code Account Creation ac = new Account Creation(); creating an instance for the above class. ac.CreateAccount('Osmania University'); calling a method system.debug(ac); check the account is created or not in the USER-DEBUG Log.
2.The following program is used fetch the Account Name from the account records by passing phone number.
Public class Fetch_Account_ Name_ From_Phone{ Public set<string> Fetch_ ACC_ Name(String P) { set <string> s1= new Set<string>(); List<Account> acc= “select id, Name from Account where phone”; for (Account a:acc) { String s=a.name; s1.add(s); } System.debug('xxxxxxxxx' +s1); return s1; } }
Go to the Developer Console, and execute the following code. FetchAccountName_ From_Phone ft= new FetchAccountName_from_Phone (); ft.FetchAccName ('9052'); In the User-Debug Log all the accounts will come that has the phone number is 9052.
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.